﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PictureChooseScript : MonoBehaviour {

    public Image ChoosedPicture;
    public Button nextBtn;
    public Button startBtn;
    public Button preBtn;
    
    private static string PicturePath = "Pictures/Start/";

	// Use this for initialization
	void Start () {
        RefreshPicture();
        nextBtn.onClick.AddListener(NextPicture);
        startBtn.onClick.AddListener(TryStart);
        preBtn.onClick.AddListener(PrePicture);
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    public void RefreshPicture()
    {
        if (ChoosedPicture != null)
        {
            Texture2D textureRes = PictureLogicScript.sGetInstance().GetTexture();
            Sprite sprite = Sprite.Create(textureRes, new Rect(0, 0, textureRes.width, textureRes.height), Vector2.zero);
            ChoosedPicture.sprite = sprite;
            PictureLogicScript.sGetInstance().SetChooseSprite(sprite);
        }
    }

    public void NextPicture()
    {
        PictureLogicScript.ImageIndex++;
        if (PictureLogicScript.ImageIndex > PictureLogicScript.MaxImageIndex)
        {
            PictureLogicScript.ImageIndex = 1;
        }
        RefreshPicture();
    }

    public void PrePicture()
    {
        PictureLogicScript.ImageIndex--;
        if (PictureLogicScript.ImageIndex < 1)
        {
            PictureLogicScript.ImageIndex = PictureLogicScript.MaxImageIndex;
        }
        RefreshPicture();
    }

    public void TryStart()
    {
        if (GameApp.GetInstance() != null)
        {
            GameApp.GetInstance().TryStart();
            GameObject.Find("AudioSource").GetComponent<AudioSource>().Play();
        }
    }
}
